﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class SportsFrontCoverDAL
    {
        private const string WAMING = "SqlServerDAL.SportsFrontCoverDAL";

        public bool Save(SportsFrontCoverDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@SfcSportId", SqlDbType.Int) {Value = dto.SfcSportId},
                                         new SqlParameter("@SfcAnnexId", SqlDbType.Int) {Value = dto.SfcAnnexId},
                                         new SqlParameter("@SfcOrder", SqlDbType.Int) {Value = dto.SfcOrder},
                                         new SqlParameter("@SfcTime", SqlDbType.DateTime) {Value = dto.SfcTime}
                                     };
                if (dto.SfcId == 0)
                {
                    sql =
                        "insert into SportsFrontCover(SfcSportId,SfcAnnexId,SfcOrder,SfcTime) values(@SfcSportId,@SfcAnnexId,@SfcOrder,@SfcTime)";
                }
                else
                {
                    sql =
                        "update SportsFrontCover set SfcSportId=@SfcSportId,SfcAnnexId=@SfcAnnexId,SfcOrder=@SfcOrder,SfcTime=@SfcTime where SfcId=@SfcId";
                    parameters.Add(new SqlParameter("@SfcId", SqlDbType.Int) { Value = dto.SfcId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".Save(SportsFrontCoverDto dto)", ex);
                return false;
            }
        }

        public List<Hashtable> SlicedFindAll(int firstResult, int maxResult, int sportId)
        {
            try
            {
                List<Hashtable> hts;
                var sql = "select row_number() over (order by SfcId) as rowNumber, * from SportsFrontCover";
                var whereSql = "";
                var parameters = new List<SqlParameter>();
                if (sportId != 0)
                {
                    whereSql = " where SfcSportId=@SfcSportId";
                    parameters.Add(new SqlParameter("@SfcSportId", SqlDbType.Int) { Value = sportId });
                }
                sql = string.Format(
                        "select * from (" + sql + whereSql + ") as SportsFrontCover where rowNumber between {0} AND {1} order by SfcId",
                        firstResult, maxResult);

                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray()))
                {
                    hts = new List<Hashtable>();
                    while (red.Read())
                    {
                        var ht = SqlHelper.FillHashtable(red);
                        hts.Add(ht);
                    }
                }
                return hts;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".SlicedFindAll(int firstResult, int maxResult)", ex);
                return null;
            }
        }
    }
}
